<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\Blog\Link;
use Illuminate\Http\Request;

class LinkService
{

    use Service;

    /**
     * 博客友情链接首页
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function LinkIndexService( $request )
    {
        $where  = function ( $query ) use ( $request ) {
            if ( $request->filled('name') ) $query->where('name', 'like', "%" . $request->input('name') . "%");
            if ( $request->filled('is_show') && $request->input('is_show') != 0 ) $query->where('is_show', $request->input('is_show'));
            $query->where('status', 1);
        };
        $with   = [
            'file' => function ( $query ) {
                $query->select('id', 'url', 'file_url');
            },
            'user' => function ( $query ) {
                $query->select('id', 'name');
            },
        ];
        $select = [ 'id', 'user_id', 'url', 'pic', 'content', 'name', 'is_show', 'status', 'created_at' ];
        $link   = Link::GetModel($where, $with, $select);
        $link->appends([
            'name'    => $request->input('name'),
            'is_show' => $request->input('is_show'),
        ])->render();
        return view('blog.link.index', compact('link', 'request'));
    }

    /**
     * 博客友情链接新增
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function LinkStoreService( $request )
    {
        $validator = $this->getAdminValidator()->LinkValidator($request);
        if ( $validator->first() ) return back()->with('error', $validator->first());
        request()->offsetSet('user_id', 0);
        Link::CreateModel($request->all());
        return back()->with('success', $this->getErrorCode()::message(209));
    }


    /**
     * 博客友情链接编辑页面
     *
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function LinkEditService( $id )
    {
        $select = [ 'id', 'url', 'pic', 'content', 'name', 'is_show', 'status' ];
        $link   = Link::FindModel($id, $select);
        return view('blog.link.edit', compact('link'));
    }

    /**
     * 博客友情链接更新
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function LinkUpdateService( $request )
    {
        $validator = $this->getAdminValidator()->LinkValidator($request);
        if ( $validator->first() ) return back()->with('error', $validator->first());
        $select = [ 'id', 'url', 'pic', 'content', 'name', 'is_show', 'status' ];
        $link   = Link::FindModel($request->input('id'), $select);
        $link->update($request->all());
        return back()->with('success', $this->getErrorCode()::message(203));
    }

    /**
     * 博客友情链接删除
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function LinkDestroyService( $id )
    {
        Link::DestroyModel($id);
        return back()->with('success', $this->getErrorCode()::message(204));
    }

    /**
     * 博客友情链接修改状态
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function LinkStatusService( $id )
    {
        $select = [ 'id', 'is_show' ];
        $link   = Link::FindModel($id, $select);
        if ( $link->is_show == 1 ) $is_show = 2;
        else $is_show = 1;
        $link->is_show = $is_show;
        $link->save();
        return back()->with('success', $this->getErrorCode()::message(210));
    }

    /**
     * 友情链接批量删除
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function LinkDeleteService( $request )
    {
        Link::DestroyModel($request->input('id'));
        return data();
    }

}
